package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OederSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：hg
 * @date ：Created in 2021-12-10 22:32
 * @description：预约设置
 */
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    //注入
    @Reference
    private OederSettingService oederSettingService;

    //上传文件，实现预约数据的批量导入
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            //使用POI解析表格数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            //创建存储表格数据对象数组
            List<OrderSetting> data = new ArrayList<OrderSetting>();
            //遍历取出一行的数据
            for (String[] strings : list) {
                String orderDate = strings[0];
                String number = strings[1];
                // 封装为一个对象，需要类型进行转换
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                //存入数组
                data.add(orderSetting);
            }
            //通过dubbo远程调用服务实现批量导入到数据库
            oederSettingService.add(data);
            //文件解析成功，返回成功信息
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            //文件解析失败,返回错误信息
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期查询预约设置数据(获取指定日期所在月份的预约设置数据)
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        try {
            List<Map> list = oederSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }

    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            oederSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }

    }
}







